import {
	apiBaseUrl
} from './config.js';
import * as common from './common.js' //引入common
import * as db from './db.js' //引入common
// 需要登陆的，都写到这里，否则就是不需要登陆的接口
const methodsToken = ['api/wx/decryptphone'];


const post = (method, data, callback, complete) => {
	uni.removeStorage({
		key: 'jump'
	})
	// 获取用户token
	let headers = {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded'
	}
	if (method=='api/member/riskrecord/evaluate' || method=='api/member/submit_question' || method=='api/verify/register' || method=='api/crisis/intervene/create'){
		headers = {
			'Accept': 'application/json',
			'Content-Type': 'application/json',
		}
	}
	
	let userToken = db.get("userToken");
	let openid = db.get("openid");
	let deptId = db.get("deptId");
	
	if (userToken) {
		headers.Authorization = userToken
	}
	if (openid) {
		headers.openid = openid
	}

	let postData = JSON.parse(JSON.stringify(data))
	if (deptId) {
		headers.deptId = deptId
		postData.deptId = deptId
	}
	if (postData.checkToken) {
		headers.Authorization = postData.checkToken
	}
	delete postData.methodType
	uni.request({
		url: apiBaseUrl + method,
		data: postData,
		header: headers,
		method: data.methodType || 'post',
		success: (response) => {
			const result = response.data
			if (!result.status) {
				// 登录信息过期或者未登录
				if (result.code === 201) {
					db.del("userToken");
					let jump = uni.getStorageSync('jump')
					console.log(jump, 1111)
					if (!jump) {
						uni.setStorageSync('jump', true)
						uni.showToast({
							title: result.message,
							icon: 'none',
							duration: 1500
						});
						setTimeout(() => {

							uni.navigateTo({
								url: '/subpage/login/login'
							})
						}, 1500)
					}
				}
			}
			callback(result);
		},

		complete: (response) => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000)
			complete ? complete() : "";
		},
		fail: (error) => {
			uni.showLoading({
				title: '网络开小差了'
			});
			// setTimeout(function() {
			uni.hideLoading();
			// }, 1000)
			if (error && error.response) {
				showError(error.response);
			} else {

			}

		},
	});

}
const get = (url, callback) => {
	uni.showLoading({
		title: '加载中'
	});
	uni.request({
		url: url,
		header: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
		},
		method: 'GET',
		success: (response) => {
			uni.hideLoading();
			callback(response.data);
		},
		fail: (error) => {
			uni.hideLoading();
			if (error && error.response) {
				showError(error.response);
			}
		},
		complete: () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 250);
		}
	});
}

const showError = error => {
	let errorMsg = ''
	switch (error.status) {
		case 400:
			errorMsg = '请求参数错误'
			break
		case 401:
			errorMsg = '未授权，请登录'
			break
		case 403:
			errorMsg = '跨域拒绝访问'
			break
		case 404:
			errorMsg = `请求地址出错: ${error.config.url}`
			break
		case 408:
			errorMsg = '请求超时'
			break
		case 500:
			errorMsg = '服务器内部错误'
			break
		case 501:
			errorMsg = '服务未实现'
			break
		case 502:
			errorMsg = '网关错误'
			break
		case 503:
			errorMsg = '服务不可用'
			break
		case 504:
			errorMsg = '网关超时'
			break
		case 505:
			errorMsg = 'HTTP版本不受支持'
			break
		default:
			errorMsg = error.msg
			break
	}

	uni.showToast({
		title: errorMsg,
		icon: 'none',
		duration: 1000,
		complete: function() {
			setTimeout(function() {
				uni.hideToast();
			}, 1000);
		}
	});
}

// 文件上传
export const uploadFiles = (callback) => {
	uni.chooseImage({
		success: (chooseImageRes) => {
			uni.showLoading({
				title: '上传中...'
			});
			const tempFilePaths = chooseImageRes.tempFilePaths;
			const uploadTask = uni.uploadFile({
				url: apiBaseUrl + 'api/file/upload', //仅为示例，非真实的接口地址
				filePath: tempFilePaths[0],
				fileType: 'image',
				name: 'file',
				header: {

					// 'Accept': 'application/json',
					// 'Content-Type': 'application/x-www-form-urlencoded',
					'Authorization': db.get("userToken")
				},
				formData: {
					'file': tempFilePaths[0],

				},
				success: (uploadFileRes) => {
					callback(JSON.parse(uploadFileRes.data));
				},
				fail: (error) => {
					if (error && error.response) {
						showError(error.response);
					}
				},
				complete: () => {
					setTimeout(function() {
						uni.hideLoading();
					}, 250);
				}
			});
		}
	});
}

// 登录
export const wxLogin = (data, callback) => post('api/wx/login', data, callback);
export const memberInfo = (data, callback) => post('api/member/info', data, callback);
export const vehicleList = (data, callback) => post('api/member/vehicle_list', data, callback);
export const vehicleSave = (data, callback) => post('api/member/save_vehicle', data, callback);
export const vehicleEdit = (data, callback) => post('api/member/update_vehicle', data, callback);
export const vehicleInfo = (data, callback) => post('api/member/vehicle_detail', data, callback);
export const myAppointInfo = (data, callback) => post('api/member/appoint_detai', data, callback);

export const vehicleDel = (data, callback) => post('api/member/delete_vehicle', data, callback);
export const appointList = (data, callback) => post('api/appoint_active/list', data, callback);
export const appointInfo = (data, callback) => post('api/appoint_active/detail', data, callback);
export const appointSave = (data, callback) => post('api/member/save_appoint', data, callback);
export const appointCancel = (data, callback) => post('api/member/cancel_appoint', data, callback);
export const appointSign = (data, callback) => post('api/member/sign_appoint', data, callback);
export const myAppointList = (data, callback) => post('api/member/appoint_list', data, callback);
export const adList = (data, callback) => post('api/ad/list', data, callback);
export const articleList = (data, callback) => post('api/article/list', data, callback);
export const articleInfo = (data, callback) => post('api/article/detail', data, callback);
export const dictList = (data, callback) => post('api/common/dict-data/list', data, callback);
export const userUpdate= (data, callback) => post('api/member/update', data, callback);
export const submit_question= (data, callback) => post('api/member/submit_question', data, callback);

// 微信获取手机号码
export const wxPhone = (data, callback) => post('api/wx/decryptphone', data, callback);

export const userUpdateMobile = (data, callback) => post('api/member/update_mobile', data, callback);

export const appoint_detail = (data, callback) => post('api/member/appoint_detail', data, callback);
export const supplierDetail = (data, callback) => post('api/supplier/detail', data, callback);
export const supplierList = (data, callback) => post('api/supplier/list', data, callback);
export const supplierActive = (data, callback) => post('api/supplier/active', data, callback);
export const supplierCity = (data, callback) => post('api/supplier/city', data, callback);

export const noticeList = (data, callback) => post('api/notice', data, callback);
export const articleCate = (data, callback) => post('api/article/category/list', data, callback);
export const articleOther = (data, callback) => post('api/promotion/articleneighbors/neighbors', data, callback);

// 时间胶囊
export const capsuleList = (data, callback) => post('api/member/date_capsule/getList', data, callback);
export const capsuleAdd = (data, callback) => post('api/member/date_capsule/add', data, callback);
export const capsuleUpdate = (data, callback) => post('api/member/date_capsule/update', data, callback);
export const capsuleDelete = (data, callback) => post('api/member/date_capsule/delete', data, callback);
export const capsuleInfo = (data, callback) => post('api/member/date_capsule/'+data.id, data, callback);

export const dayReservationList = (data, callback) => post('api/system/day_reservation/list', data, callback);
// 预约
export const reservationOneList = (data, callback) => post('api/member/reservation_one/list', data, callback);
export const reservationOneAdd = (data, callback) => post('api/member/reservation_one/add', data, callback);
export const reservationOneInfo = (data, callback) => post('api/member/reservation_one/'+data.id, data, callback);
export const reservationOneToEx = (data, callback) => post('api/member/reservation_one/toEx', data, callback);
export const reservationOneToUp = (data, callback) => post('api/member/reservation_one/toUp', data, callback);

export const reservationGroupList = (data, callback) => post('api/member/reservation_group/list', data, callback);
export const reservationGroupAdd = (data, callback) => post('api/member/reservation_group/add', data, callback);
export const reservationGroupInfo = (data, callback) => post('api/member/reservation_group/'+data.id, data, callback);
export const reservationGroupToEx = (data, callback) => post('api/member/reservation_group/toEx', data, callback);
export const reservationGroupToUp = (data, callback) => post('api/member/reservation_group/toUp', data, callback);

// 问卷
export const questionList = (data, callback) => post('api/question/list', data, callback);
export const questionInfo = (data, callback) => post('api/question/info', data, callback);
export const questionTopic= (data, callback) => post('api/question/topic', data, callback);

// 登录
export const verifyLogin= (data, callback) => post('api/verify/login', data, callback);
export const sendCode= (data, callback) => post('api/common/sendCode', data, callback);
// 注册
export const verifyRegister= (data, callback) => post('api/verify/register', data, callback);
export const schoolList= (data, callback) => post('api/common/school/list', data, callback);
export const gradeList= (data, callback) => post('api/common/grade/list', data, callback);
export const classesList= (data, callback) => post('api/common/classes/list', data, callback);
export const loginDept= (data, callback) => post('api/verify/login/dept', data, callback);
// 危机干预
export const crisisAdd= (data, callback) => post('api/crisis/management/create', data, callback);
export const crisisList= (data, callback) => post('api/crisis/management/list', data, callback);
export const updateLevel= (data, callback) => post('api/crisis/management/update-level', data, callback);
export const crisisInfo= (data, callback) => post('api/crisis/management/get', data, callback);

export const interveneConfig= (data, callback) => post('api/crisis/intervene/config', data, callback);

export const interveneCreate= (data, callback) => post('api/crisis/intervene/create', data, callback);
export const interveneUpdateEnded= (data, callback) => post('api/crisis/management/update-ended', data, callback);
export const interveneHistory= (data, callback) => post('api/crisis/intervene/history', data, callback);

export const interveneInfo= (data, callback) => post('api/crisis/intervene/get', data, callback);

export const dossierRecordList= (data, callback) => post('api/crisis/management/dossier/record-list', data, callback);

export const dossierList= (data, callback) => post('api/crisis/management/dossier/list', data, callback);

export const tasksList= (data, callback) => post('api/crisis/tasks/list', data, callback);

export const tasksCompleted= (data, callback) => post('api/crisis/tasks/form/completed', data, callback);


export const evaluationtask= (data, callback) => post('api/member/evaluationtask/list', data, callback);

export const evaluationtaskInfo= (data, callback) => post('api/member/evaluationtask/detail', data, callback);

export const reportList= (data, callback) => post('api/member/report/list', data, callback);

export const reportInfo= (data, callback) => post('api/member/report/detail', data, callback);

export const profilesList= (data, callback) => post('api/member/profiles/list', data, callback);

export const riskrecordList= (data, callback) => post('api/member/riskrecord/list', data, callback);


export const riskrecordDetail= (data, callback) => post('api/member/riskrecord/detail', data, callback);

export const riskrecordEvaluate= (data, callback) => post('api/member/riskrecord/evaluate', data, callback);
